﻿package  
{
	import com.adamatomic.flixel.*;

	public class PlaybackLOD
	{
		// Image data used by this class
		[Embed(source = "../data/playercontrols_plus.png")] private var mImgPlus:Class;
		[Embed(source = "../data/playercontrols_plus_selected.png")] private var mImgPlusSelected:Class;
		[Embed(source = "../data/playercontrols_minus.png")] private var mImgMinus:Class;
		[Embed(source = "../data/playercontrols_minus_selected.png")] private var mImgMinusSelected:Class;
		[Embed(source = "../data/playercontrols_divider.png")] private var mImgDivider:Class;
		[Embed(source = "../data/playercontrols_stop.png")] private var mImgStop:Class;
		
        // Constants defining the different types of playback LOD the solver
        // can use.
        //
        public static const PLAYBACK_LOD_ONE_CELL:Number   = 0;
        public static const PLAYBACK_LOD_ONE_PASS:Number   = 1;
        public static const PLAYBACK_LOD_FULL_PASS:Number  = 2;
		public static const PLAYBACK_LOD_NUM:Number		   = 2;
        
        // Stores the type of playback LOD on the solver.  This controls how
        // much of the board is updated between render passes.  These are defined
        // by the constants above.
        //
        private var mPlaybackLOD:Number;
		
		// The sprite which shows what the current LOD is.
		//
		private var mLODDisplay:FlxSprite;
		
		// The images used on the LOD toggle button.
		//
		private var mPlus:FlxSprite;
		private var mPlusSelected:FlxSprite;
		private var mDivider:FlxSprite;
		private var mMinus:FlxSprite;
		private var mMinusSelected :FlxSprite;
		
		// The buttons that make up the LOD toggle.
		//
		private var mPlusButton:FlxButton;
		private var mMinusButton:FlxButton;
		
		// Constructor
		//
		public function PlaybackLOD( state:FlxState ) 
		{
			// Create the display for the LOD display
			mLODDisplay = state.add( new FlxSprite( mImgStop, 59, 227, true, false, 159 ) ) as FlxSprite;
			
			// All the sprites used to make the +/- button
			//
			mPlus 			= new FlxSprite( mImgPlus);
			mPlusSelected 	= new FlxSprite( mImgPlusSelected);
			mDivider 		= new FlxSprite( mImgDivider, 242, 227 );
			mMinus 			= new FlxSprite( mImgMinus);
			mMinusSelected 	= new FlxSprite( mImgMinusSelected);
			
			// The divider is just a sprite.
			state.add( mDivider );
			
			// We have a seperate button for both Plus and Minus
			mMinusButton    = state.add( new FlxButton( 242, 227, mMinus, DecrementLOD, mMinusSelected ) ) as FlxButton;
			mPlusButton     = state.add( new FlxButton( 257, 227, mPlus, IncrementLOD, mPlusSelected ) ) as FlxButton;
			
			// Default to one full pass.
			SetCurrentLOD( PLAYBACK_LOD_ONE_CELL );			
		}
		
		// Accessor for the current LOD playback setting.
		//
		public function GetCurrentLOD( ) : Number
		{
			return mPlaybackLOD;
		}
		
		// Sets the current LOD and updates the display as needed.  Ignores
		// any invalid or redundant calls.
		//
		public function SetCurrentLOD( newLOD:Number ) : void
		{
			if ( newLOD == mPlaybackLOD || newLOD < 0 || newLOD > PLAYBACK_LOD_NUM )
			{
				return;
			}
			
			mPlaybackLOD = newLOD;
			
			UpdateLODController( );
		}
		
		// Updates which buttons are enabled on the LOD controller to 
		// properly reflect the current LOD.
		//
		private function UpdateLODController( ) : void
		{
			// We have direct access to the LOD but we may eventualy
			// do some data massaging in the accessor function, so let's
			// get access through it.
			var LOD:Number = GetCurrentLOD( );
			
			// The frames of the sprint corrispond to the LOD constants.
			mLODDisplay.specificFrame( LOD );
			
			switch( GetCurrentLOD( ) )
			{
				case PLAYBACK_LOD_ONE_CELL:
				{
					mPlusButton.visible = true;
					mMinusButton.visible = false;
					break;
				}
				case PLAYBACK_LOD_ONE_PASS:
				{
					mPlusButton.visible = true;
					mMinusButton.visible = true;
					break;
				}
				case PLAYBACK_LOD_FULL_PASS:
				{
					mPlusButton.visible = false;
					mMinusButton.visible = true;
					break;
				}
			}
		}
		
        // Changes the LOD playback moving up.
        //
		private function IncrementLOD( ) : void
		{
			SetCurrentLOD( GetCurrentLOD( ) + 1 );
		}
		
        // Changes the LOD playback moving down.
        //
		private function DecrementLOD( ) : void
		{
			SetCurrentLOD( GetCurrentLOD( ) - 1 );
		}
        
        // Disables all the buttons for this control.
        //
        public function DisableButtons( ) : void
        {
            mPlusButton.active = false;
            mMinusButton.active = false;
        }
        
        // Enables previously disabled buttons on this control.
        //
        public function EnableButtons( ) : void
        {
            mPlusButton.active = true;
            mMinusButton.active = true;
        }
	}
}